package org.kjtc.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.kjtc.entity.DeviceObject;
import org.kjtc.entity.Page;
import org.kjtc.entity.StatisticsReport;
import org.kjtc.service.StatisticsReportService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class StatisticsReportController extends BaseController {

	@Resource(name = "deviceObject")
    private DeviceObject deviceObject;
	
	@Resource(name = "statisticsReportService")
    private StatisticsReportService statisticsReportService;
	
	@RequestMapping(value = "/searchStatisticsReport")
	@ResponseBody
	public Map<String, Object> searchStatisticsReport(StatisticsReport statisticsReport, String page, String rows) {
		
		Map<String, Object> map = new HashMap<String, Object>();
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "统计报表信息查询");
		// 分页
		Page pageQuery = getPage();
		pageQuery.setCurrentPage(Integer.parseInt(page));
		pageQuery.setCurrentResult((Integer.parseInt(page) - 1) * (Integer.parseInt(rows)));
		pageQuery.setShowCount(Integer.parseInt(rows));
		
		List<StatisticsReport> list = null;
		
		try {
			list = statisticsReportService.getStatisticsReportPage(statisticsReport, pageQuery);
			map.put("totalCnt", pageQuery.getTotalResult());
			map.put("totalPage", pageQuery.getTotalPage());
			map.put("statisticsReportList", list);
			map.put("result", "Success");
			statisticsReport = null;
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "统计报表信息查询异常" + e.getMessage());
			map.put("result", "Error");
		}
		return map;
	}
	
	/*
	 * 故障信息添加
	 */
	@RequestMapping(value = "/saveStatisticsReport")
	@ResponseBody
	public Map<String, Object> saveStatisticsReport(StatisticsReport statisticsReport) {

		Map<String, Object> map = new HashMap<String, Object>();
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "统计报表信息添加");
		statisticsReport.setCreateUser(this.getUesr().getUserID());
		statisticsReport.setUpdateUser(this.getUesr().getUserID());
		int count = 0;
		try {
			count = statisticsReportService.saveStatisticsReport(statisticsReport);
			if (count > 0) {
				map.put("result", "Success");
			} else {
				map.put("result", "NotFound");
			}
		} catch (Exception e) {
			e.printStackTrace();
			map.put("result", "Error");
		    logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "统计报表信息添加异常" + e.getMessage());
		}
		statisticsReport = null;
		return map;
	}
	
	/*
	 * 故障信息修改
	 */
	@RequestMapping(value = "/updateStatisticsReport")
	@ResponseBody
	public Map<String, Object> updateStatisticsReport(StatisticsReport statisticsReport) {

		Map<String, Object> map = new HashMap<String, Object>();
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "统计报表信息修改");
		statisticsReport.setUpdateUser(this.getUesr().getUserID());
		int count = 0;
		try {
			count = statisticsReportService.updateStatisticsReport(statisticsReport);
			if (count > 0) {
				map.put("result", "Success");
			} else {
				map.put("result", "NotFound");
			}
		} catch (Exception e) {
			e.printStackTrace();
			map.put("result", "Error");
		    logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "统计报表信息修改异常" + e.getMessage());
		}
		statisticsReport = null;
		return map;
	}
	
	/*
	 * 故障信息删除
	 */
	@RequestMapping(value = "/deleteStatisticsReport")
	@ResponseBody
	public Map<String, Object> deleteStatisticsReport(String id) {

		Map<String, Object> map = new HashMap<String, Object>();
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "统计报表信息删除");
		String[] idList = id.split(",");
		StatisticsReport statisticsReport = new StatisticsReport();
		statisticsReport.setIDList(idList);
		int count;
		try {
			count = statisticsReportService.deleteStatisticsReport(statisticsReport);
			if (count > 0) {
				map.put("result", "Success");
			} else {
				map.put("result", "NotFound");
			}
		} catch (Exception e) {
			e.printStackTrace();
			map.put("result", "Error");
			logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "统计报表信息删除异常:" + e.getMessage());
		}
		statisticsReport = null;
		return map;
	}
	
	@RequestMapping(value = "/exportStatisticsReport")
	@ResponseBody
	public Map<String, Object> exportStatisticsReport(StatisticsReport statisticsReport) {
		
		Map<String, Object> map = new HashMap<String, Object>();
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "统计报表信息查询");
		
		List<Object> list = null;
		
		try {
			list = statisticsReportService.getStatisticsReport(statisticsReport);
			
			//生成报表
			map.put("result", "Success");
			statisticsReport = null;
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "统计报表信息查询异常" + e.getMessage());
			map.put("result", "Error");
		}
		return map;
	}
}
